package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Billet;
import dao.DaoBillet;

/**
 * Servlet implementation class GetBillet
 */
public class GetBillet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetBillet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		DaoBillet daobillet = new DaoBillet();
		List<Billet> listebillet = new ArrayList<Billet>();
			
		if(request.getParameter("date_arrivee") == null){
			
			String ville_dep = request.getParameter("ville_dep");
			String ville_arr = request.getParameter("ville_arr");
			
			Double prix_m = 0.0;
			try {
				prix_m = Double.parseDouble(request.getParameter("prix_m"));
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			listebillet = daobillet.searchBillet(
					ville_dep, 
					ville_arr,				
					prix_m);			
		}
		else{
			Double prix_max = 0.0;
			try {
				prix_max=Double.parseDouble(request.getParameter("prix_max"));
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			listebillet = daobillet.searchAdvancedBillet(
				request.getParameter("ville_depart"), 
				request.getParameter("ville_arrivee"), 
				request.getParameter("date_depart"), 
				request.getParameter("date_arrivee"), 
				prix_max);
		}
		
		request.setAttribute("liste_billet", listebillet );
		request.getRequestDispatcher("/liste_billet.jsp").forward( request, response );
	}

}
